#include <iostream>
#include <string>
#include <memory>
#include <functional>
static const std::string serverip = "127.0.0.1";
static const int default_retry_time = 1;
static const int default_max_retrynums = 5;
enum class Status//c++11强类型枚举
{
    NEW,        // 新建连接
    CONNECTED,  // 连接成功
    DISCONNECT, // 重连失败
    CLOSE       // 关闭连接
};

using Func = std::function<void()>;

class TcpConnection
{
public:
    TcpConnection(const std::string &ip, uint16_t port, Func func)
        : _serverip(ip),
          _serverport(port),
          _status(Status::NEW),
          _time_retry(default_retry_time),
          _maxConnectNums(default_max_retrynums),
          _func(func)
    {
    }
    void Connection();
    void ReConnection();
    void DisConnection();
    void Task()
    {
        _func();
    }
    Status ConnectionStatus()
    {
        return _status;
    }
    ~TcpConnection() {}

private:
    std::string _serverip;
    uint16_t _serverport;
    Status _status;      // 连接状态
    int _time_retry;     // 尝试重连的时间
    int _maxConnectNums; // 最大的重连次数
    Func _func;
};
class TcpClient
{
public:
    TcpClient(const std::string& ip,uint16_t port,Func func)
    :_connection(ip,port,func)
    {}
    ~TcpClient() {}
    void Execute()
    {
        while (true)
        {
            switch (_connection.ConnectionStatus())
            {
            case Status::NEW:
                _connection.Connection();
                break;
            case Status::CONNECTED:
                _connection.Task();
                break;
            case Status::DISCONNECT:
                _connection.ReConnection();
                break;
            case Status::CLOSE:
                _connection.DisConnection();
                return;
            default:
                //啥都不用做
                break;
            }
        }
    }

private:
    TcpConnection _connection;
};
// usage Client port
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        // Lambda表达式
        ([]()
         { std::cerr << "Usage: ./Client port" << std::endl; })();
        exit(0);
    }
    uint16_t serverport = std::stoi(argv[1]);
    std::unique_ptr<TcpClient> client = std::make_unique<TcpClient>(serverip,serverport,[]()
    {
        
    });
    client->Execute();
    return 0;
}